<?php

namespace app\common\model;

use think\Model;

/**
 * 邮箱验证码
 */
class Materials Extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    // 追加属性
    protected $append = [
    ];
    // 获取文章下面的图文
    public function getMaterialItems($material_id){
    	$items = db('materials')->alias('a')
    					->join("material_mate m","m.news_id = a.id")
    					->where(['m.material_id'=>$material_id])
    					->field("a.*")
    					->order('sort asc')
    					->select();
    	return empty($items) ? [] : $items;
    }	
    // 图文链接
    public function getArticles($id=null){
        $articles = [];
        $row = db('materials')->where('id',$id)->find();
        if(!empty($row)){
            $articles[] = array(
                'Title' => $row['title'],
                'Description' => $row['digest'],
                'PicUrl' => request()->domain().$row['thumb'],
                'Url' => (empty($row['content_source_url']) ? request()->domain().'/index/article/detail?article_id='.$id : $row['content_source_url']),
            );
            $items = $this->getMaterialItems($id);
            if(!empty($items)){
                foreach($items as $item){
                     $articles[] = array(
                        'Title' => $item['title'],
                        'Description' => $item['digest'],
                        'PicUrl' => request()->domain().$item['thumb'],
                        'Url' => (empty($item['content_source_url']) ? request()->domain().'/index/article/detail?article_id='.$item['id'] : $item['content_source_url']),
                    );
                }
            }
        }
        return $articles;
    }
}
